package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.entity.Order;
import com.nest.WorkNest.tools.Tool;
import com.nest.WorkNest.view.GridViewForScrollView;
import com.orhanobut.logger.Logger;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class EnquiryDetailAct extends BaseActivity implements OnClickListener {
    @Bind(R.id.btn_enquirydetail_recommend)
    Button btnEnquirydetailRecommend;
    @Bind(R.id.btn_enquirydetail_inquireDevelop)
    Button btnEnquirydetailInquireDevelop;
    @Bind(R.id.ll_enquirydetail_quotation)
    LinearLayout llEnquirydetailQuotation;
    @Bind(R.id.tv_enquirydetail_id)
    TextView tvEnquirydetailId;
    @Bind(R.id.tv_enquirydetail_name)
    TextView tvEnquirydetailName;
    @Bind(R.id.tv_enquirydetail_demo)
    TextView tvEnquirydetailDemo;
    GridViewForScrollView enquirydetailItems;
    @Bind(R.id.tv_enquirydetail_FAA)
    TextView tvEnquirydetailFAA;
    @Bind(R.id.tv_enquirydetail_AR)
    TextView tvEnquirydetailAR;
    @Bind(R.id.ll_enquirydetail_AR)
    LinearLayout llEnquirydetailAR;
    @Bind(R.id.tv_enquirydetail_CL)
    TextView tvEnquirydetailCL;
    @Bind(R.id.tv_enquirydetail_pack)
    TextView tvEnquirydetailPack;
    @Bind(R.id.tv_enquirydetail_Time)
    TextView tvEnquirydetailTime;
    @Bind(R.id.tv_enquirydetail_bigTime)
    TextView tvEnquirydetailBigTime;
    @Bind(R.id.tv_enquirydetail_FON)
    TextView tvEnquirydetailFON;
    @Bind(R.id.tv_enquirydetail_PL)
    TextView tvEnquirydetailPL;
    @Bind(R.id.tv_enquirydetail_PD)
    TextView tvEnquirydetailPD;
    @Bind(R.id.tv_enquirydetail_ordertime)
    TextView tvEnquirydetailOrdertime;
    @Bind(R.id.sv_enquirydetail)
    ScrollView svEnquirydetail;
    private String inquireId, QuoId, inquireOrderStatus, productName, imgUrl;
    private Context context = EnquiryDetailAct.this;
    private HashMap<String, String> data = new HashMap<>();
    private HolderAdapter<HashMap<String, String>> mAdapter;
    private List<HashMap<String, String>> datalist = new ArrayList<HashMap<String, String>>();
    private List<HashMap<String, String>> quotelist = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_enquirydetail);
        ButterKnife.bind(this);
        initBar();
        inquireId = getIntent().getStringExtra("inquireId");
        loadInquireId();
        enquirydetailItems = (GridViewForScrollView) findViewById(R.id.enquirydetail_items);
        enquirydetailItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        if (!TextUtils.isEmpty(getIntent().getStringExtra("imgUrl"))) {
            imgUrl = getIntent().getStringExtra("imgUrl");
            Logger.d(imgUrl);
        }
        initAdapter();
        svEnquirydetail.scrollTo(0, 0);
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("询价单详情");
        tv_actionbar_tab_side_right.setVisibility(View.VISIBLE);
        tv_actionbar_tab_side_right.setText("状态记录");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        llEnquirydetailQuotation.setOnClickListener(this);
        tv_actionbar_tab_side_right.setOnClickListener(this);
        btnEnquirydetailInquireDevelop.setOnClickListener(this);
        tabBack();
    }


    public void loadInquireId() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack iOAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                JSONObject jsonObject = Tool
                        .getJsonObject(result);
                if (Tool.getString(jsonObject, "resultCode").equals("0")) {
                    data = Tool.getMapByJsonObj(Tool.getString(jsonObject, "inquire"));
                    tvEnquirydetailId.setText(data.get(Order.inquireId));
                    tvEnquirydetailBigTime.setText(data.get(Order.bigTime));

//                    tvEnquirydetailCL.setText(data.get(Order.Cl));
//                    tvEnquirydetailDemo.setText(data.get(Order.demo));
//                    tvEnquirydetailFAA.setText(data.get(Order.FAA));
//                    tvEnquirydetailFON.setText(data.get(Order.FON));
//                    tvEnquirydetailPack.setText(data.get(Order.pack));
                    if ("1".equals(data.get(Order.demo))) {
                        tvEnquirydetailDemo.setText("是");
                    } else if ("0".equals(data.get(Order.demo))) {
                        tvEnquirydetailDemo.setText("否");
                    } else {
                        tvEnquirydetailDemo.setText(data.get(Order.demo));
                    }
                    if ("1".equals(data.get(Order.FON))) {
                        tvEnquirydetailFON.setText("是");
                    } else if ("0".equals(data.get(Order.FON))) {
                        tvEnquirydetailFON.setText("否");
                    } else {
                        tvEnquirydetailFON.setText(data.get(Order.FON));
                    }
                    if ("10".equals(data.get(Order.FAA))) {
                        tvEnquirydetailFAA.setText("包工包料");
                    } else if ("11".equals(data.get(Order.FAA))) {
                        tvEnquirydetailFAA.setText("清加工");
                    } else if ("12".equals(data.get(Order.FAA))) {
                        tvEnquirydetailFAA.setText("我指定面料供应商");
                    } else {
                        tvEnquirydetailFAA.setText(data.get(Order.FAA));
                    }

                    if ("20".equals(data.get(Order.Cl))) {
                        tvEnquirydetailCL.setText("我提供");
                    } else if ("21".equals(data.get(Order.Cl))) {
                        tvEnquirydetailCL.setText("不需要");
                    } else if ("22".equals(data.get(Order.Cl))) {
                        tvEnquirydetailCL.setText("委托采购");
                    } else {
                        tvEnquirydetailCL.setText(data.get(Order.Cl));
                    }
                    if ("30".equals(data.get(Order.pack))) {
                        tvEnquirydetailPack.setText("我提供");
                    } else if ("31".equals(data.get(Order.pack))) {
                        tvEnquirydetailPack.setText("不需要");
                    } else if ("32".equals(data.get(Order.pack))) {
                        tvEnquirydetailPack.setText("委托采购");
                    } else {
                        tvEnquirydetailPack.setText(data.get(Order.pack));
                    }
                    tvEnquirydetailTime.setText(data.get(Order.predictPerOrderNumber));
                    tvEnquirydetailName.setText(data.get(Order.inquirename));
                    productName = data.get(Order.inquirename);
                    tvEnquirydetailPL.setText(data.get(Order.PL));
                    tvEnquirydetailPD.setText(data.get(Order.PD));
                    tvEnquirydetailOrdertime.setText(data.get("date"));
                    btnEnquirydetailRecommend.setText(data.get(Order.statusNote));
                    JSONArray imgArray = Tool.getJsonArray(jsonObject, "imageList");
                    inquireOrderStatus = data.get("inquireOrderStatus");
                    if (inquireOrderStatus.equals("1106")) {
                        btnEnquirydetailInquireDevelop.setVisibility(View.VISIBLE);
                    }
                    JSONArray quoteArray = Tool.getJsonArray(result, "quoteList");
                    quotelist = Tool.getListMapByJsonArray(quoteArray);
                    if (quotelist.size() != 0) {
                        QuoId = quotelist.get(0).get("quoteId");
                        if (!TextUtils.isEmpty(QuoId)) {
                            llEnquirydetailQuotation.setVisibility(View.VISIBLE);
                        }
                    }
                    datalist = Tool.getListMapByJsonArray(imgArray);
                    mAdapter.update(datalist);
                    if (data.get(Order.FAA).equals("19"))
                        tvEnquirydetailAR.setText(data.get(Order.AR));
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {
            }
        };
        xutilsPost.doPostEnquiryDetail(context, inquireId, iOAuthCallBack);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_enquirydetail_quotation:
                Intent intent = new Intent(context, QuotationDetailAct.class);
                intent.putExtra("quoteId", QuoId);
                startActivity(intent);
                break;
            case R.id.btn_enquirydetail_inquireDevelop:
                loadinquireDevelop();
                break;
            case R.id.tv_actionbar_tab_side_right:
                Intent intent1 = new Intent(context, StatusRecordAct.class);
                intent1.putExtra("bulkOrderId", inquireId);
                intent1.putExtra("productName", productName);
                intent1.putExtra("imgUrl", imgUrl);
                startActivity(intent1);
                break;
        }
    }

    public void initAdapter() {
        HolderAdapter.ViewCreator<HashMap<String, String>> creator = new HolderAdapter.ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {

                return LayoutInflater.from(context).inflate(R.layout.items_gridview_enquirydetail, null);
            }

            @Override
            public void updateView(View view, int position, HashMap<String, String> map) {
                ImageView pic = (ImageView) view.findViewById(R.id.iv_enquirydetail_pic);
                String imgUrl = datalist.get(position).get("url");
                if (!TextUtils.isEmpty(imgUrl)) {
                    if (!imgUrl.substring(0, 1).equals("h") && !imgUrl.substring(0, 1).equals("H")) {
                        imgUrl = "http://" + imgUrl;
                    }
                }
                Glide.with(context)
                        .load(imgUrl)
                        .centerCrop()
                        .placeholder(R.drawable.empty_photo)
                        .crossFade()
                        .into(pic);
            }
        };
        mAdapter = new HolderAdapter<>(this.getLayoutInflater(), creator);
        enquirydetailItems.setAdapter(mAdapter);
        mAdapter.update(datalist);
    }

    public void loadinquireDevelop() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                Logger.json(result);
                if (Tool.getString(result, "resultCode").equals("0")) {
                    showToast("转开发成功");
                    finish();
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostinquireDevelop(context, inquireId, ioAuthCallBack);
    }

//    protected void onResume() {
//        loadInquireId();
//        super.onResume();
//    }
}
